package org.spring.springboot.exception.common.ascept;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//@Aspect
//@Component
public class MethodAscept {

    private final static Logger LOGGER = LoggerFactory.getLogger(MethodAscept.class);

    @Autowired
    private ExceptionHandle exceptionHandle;

    @Pointcut("execution(public * org.spring.springboot.exception.controller.*.*(..))")
    public void log(){

    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        //class_method
        LOGGER.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName() + "," + joinPoint.getSignature().getName());
        //args[]
        LOGGER.info("args={}",joinPoint.getArgs());
    }

    @Around("log()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    	
        Object result = null;
        try {
        	//中间执行具体方法
        	result = proceedingJoinPoint.proceed();
        	
        } catch (Exception e) {
            return exceptionHandle.exceptionGet(e);
        }
        
        if (result == null) {
        	result="无返回值！";
        }
        return result;
    }

    @AfterReturning(pointcut = "log()",returning = "object")//打印输出结果
    public Object doAfterReturing(Object object){
        LOGGER.info("response={}",object.toString());
        return object;
    }
}